/**
 * 功能：主页面显示处理 date：2011-12-06 author: zhangzhijian
 */
Ext.onReady(function() {

	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

	var menuPanel = new Ext.Panel({
				frame : 'true',
				region : 'west',
				id : 'menuPanel', // see Ext.getCmp() below
				title : '',
				split : true,
				width : 200,
				minSize : 175,
				maxSize : 400,
				collapsible : true,
				margins : '0 0 0 5',
				layout : {
					type : 'accordion',
					animate : true
				}
			});

	var tabPanel = new Ext.TabPanel({
				id : 'masterPanel',
				region : "center",
				defaults : {
					autoScroll : true
				}, // here
				frame : true,
				activeTab : 0,
				items : [{
							id : 'smsDesktop',
							title : '短信平台桌面',
							closable : false
						}]
			});

	var viewport = new Ext.Viewport({
				layout : 'border',
				items : [
						// create instance immediately
						 new Ext.BoxComponent({
						 region: 'north',
						 height: 30, // give north and south regions a height
						 autoEl: {
						 tag: 'div',
						 html:'<div id="box"><div class="b1"></div><div id="b2"> </div><div id="b3">当前登陆：' + petName + '</div></div>'
						 }
						 }),
						menuPanel, tabPanel]
			});

	// 创建Tab标签页
	var addTab = function(id, title, url) {
		var newId = 'tab_' + id;
		// 判断Tab标签页是否已经打开
		var tab = tabPanel.getItem(newId);
		if (tab) {
			tabPanel.setActiveTab(tab);
			return;
		}
		var newTab = tabPanel.add({
					id : id,
					title : title,
					layout : 'fit',
					closable : true,
					html : "<iframe width='100%' height='100%' scrolling='auto' "
							+ "frameborder='0' src='" + url + "'></iframe>"
				});
		tabPanel.setActiveTab(newTab);
	};

	// 获取系统菜单数据
	var _ROOT_ID = "000"; // 根节点ID
	var _fields = {
		id : 'id',
		menuname : 'menuname',
		parentmenuid : 'parentmenuid',
		showorder : 'showorder',
		menuimage : 'menuimage',
		url : 'url'
	};

	function toStoreFields(fields) {
		var array = new Array();
		var i = 0;
		for (var prop in fields) {
			array[i++] = prop;
		}
		return array;
	}

	var menuStore = new Ext.data.JsonStore({
				url : './home_home_getSysMenu.htm',
				fields : toStoreFields(_fields),
				listeners : {
					'load' : function(store, records, options) {
						doMenu();
					}
				}
			});
	menuStore.load();

	var createMenu = function(pm) {
		var itemPanel = new Ext.Panel({
					title : pm.get(_fields.menuname),
					border : false
				});
		var items = menuStore.query(_fields.parentmenuid, pm.get(_fields.id),false,false);
		var noteArray = new Array();
		for (var k = 0; k < items.getCount(); k++) {
			var node = new Ext.tree.TreeNode({
						id : items.get(k).get(_fields.id),
						text : items.get(k).get(_fields.menuname),
						url : items.get(k).get(_fields.url),
						leaf : true
					});
			noteArray[k] = node;
		}
		var rootNode = new Ext.tree.AsyncTreeNode({
					text : 'test',
					expanded : true,
					children : noteArray
				});
		var item = new Ext.tree.TreePanel({
					border : false,
					rootVisible : false,
					root : rootNode
				});
		item.on("click", function(node, event) {
					addTab(node.id, node.text, node.attributes.attributes.url);
				});
		itemPanel.add(item);
		return itemPanel;
	};

	var doMenu = function() {
		var pms = menuStore.query(_fields.parentmenuid, _ROOT_ID);
		for (var j = 0; j < pms.getCount(); j++) {
			var navBar = createMenu(pms.get(j));
			menuPanel.add(navBar);
		}
		menuPanel.doLayout();
	};
});